<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
    <h1>Gebouw-detail bewerken</h1>
</hgroup>
<?php if(!isset($_POST['Submit'])) { 
    
    // A check if the id is still a numeric character and hasn't been changed manually
    if (!is_numeric($_GET['id'])) { 
        header("Location: ".$cfg['basepath']."controller/gebouw/gebouw.php");
    }

    $getBuildingSpec = GetBuildingSpecById($_GET["id"]);
    while ($buildingspecRow = mysql_fetch_array($getBuildingSpec))
    {
        $Id = $_GET['id'];
        $BuildingKey = $buildingspecRow['@buildingkey'];
        $Comment = $buildingspecRow['@comment'];
        $required = $buildingspecRow['@required'];
    }

    
    ?>
<form  name="buildingdetailform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
        <tr>
            <td>Gegevens-veld</td>
            <td><input required placeholder="bv: breedte gebouw" name="detail" value="<?php echo $BuildingKey; ?>" type="text"></td>
        </tr>
        <tr>
            <td>Commentaar</td>
            <td><input required placeholder="bv: breedte in meter" value="<?php echo $Comment; ?>" name="comment" type="text"></td>
        </tr>
        <tr>
            <td>Verplicht:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="verplicht" class="onoffswitch-checkbox" id="verplicht" <?php if ($required == "T") { echo "checked"; } ?>>
                    <label class="onoffswitch-label" for="verplicht">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
 	<tr>
            <td><input type="submit" name="Submit" value="Aanpassen">
            <input type="hidden" name="detailid" value="<?php echo $Id ?>" />
            <input type="hidden" name="start" value="<?php if (isset($_GET['start'])){ echo $_GET['start']; } else { echo 0; }  ?>" /></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
    <?php 
} 
else {
    
    $error = array();
        
        if($_POST['detail'] == "") {
		$error[] = 'Het gegevens-veld is niet ingevuld.';
	}
        
        if($_POST['comment'] == "") {
		$error[] = 'Je moet extra commentaar toevoegen.';
	}
        
        if (isset($_POST['verplicht']))
        {
            $verplicht = "T";
        }
        else
        {
            $verplicht = "F";
        }
        
        $errors = sizeof($error); // count the amount of errors
        if($errors != 0) { // if there is atleast 1 error, display the error-list
                echo "<span style=\"color:red\">Dit gebouw-detail kan niet worden aangepast omwille van de volgende reden(en):</span>";
                echo "<ul>";
                        for($i = 0; $i < $errors; $i++) {
                                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                        }
                echo "</ul>";
        } 
        else 
        {            
            UpdateBuildingSpec($_POST['detailid'], $_POST['detail'], $verplicht, $_POST['comment']);
            echo "Het gebouw-detail is aangepast.<br>";
            echo "Klik <a href=\"gebouwdetailoverzicht.php?start=".$_POST['start']."\">hier</a> om terug te gaan naar het overzicht.";
        }   
}
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>